package sc.common.test.hopper.handler.util;

import java.io.UnsupportedEncodingException;

import org.jboss.netty.buffer.ChannelBuffer;
import static org.jboss.netty.buffer.ChannelBuffers.*;
import static sc.common.test.hopper.Consts.*;

public class BufferUtil {
	public static void setRemainSizeOnHead(ChannelBuffer tobuf){
		tobuf.setIndex(0, tobuf.readableBytes() - INT_SIZE);
	}
	
	public static void writeUTF8(ChannelBuffer tobuf, String data){
		ChannelBuffer temp = dynamicBuffer();
		temp.writeInt(0);
		try {
			temp.writeBytes(data.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		setRemainSizeOnHead(temp);
		tobuf.writeBytes(temp);
	}
}
